package core.weapons;

import graphics.GlowParticle;
import graphics.SparkParticle;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import core.Player;
import core.units.Unit;
import core.world.CollisionHandler;


/**
 * 
 * @author Anton
 * BROKEN
 */
public class Laser extends Attack{
	private int maxLifetime=200;
	private double sweepFrame;
	private double wallX, wallY, tx, ty, dx, dy;
	
	
	public Laser(double inDir, double sweep, Player m){
		super(m.getHero().getPoint(), m);
		lifetime=maxLifetime;
		dir=inDir-sweep/2;
		sweepFrame=sweep/maxLifetime;
		speed=5000;
		damage=2;
	}
	
	public void checkHit(Unit u){
		hasHit=false;
		double tempx=x;
		double tempy=y;
		if(u.getTeam()!=team){
			while(!hasHit){	
				tempx=tempx+dx;
				tempy=tempy+dy;
				
				//main.particles.add(new GlowParticle((int)tempx, (int)tempy));
				//if (u.bounds.contains(tempx/FACTOR, tempy/FACTOR)){
				if (u.bounds.contains(tempx, tempy)){
					u.takeDamage(damage);
					hasHit=true;
					tx=tempx;
					ty=tempy;
					//System.out.print("i");
				}
				if( (tempx>tx && dx>0) || (tempx<tx && dx<0) || (tempy>ty && dy>0) || (tempy<ty && dy<0)){
					hasHit=true;
				}
			}		
		}			
	}
	
	public void paint(Graphics g){
		g.setColor(Color.blue);
		g.drawLine((int)x, (int)y, (int)tx, (int)ty);
		
	}
	
	public void paint(Point screenPos, int zoomDivider, Graphics g){
		g.setColor(Color.blue);
		//int size=4000;
		//g.drawLine((x-screenPos.x)/zoomDivider, (y-screenPos.y)/zoomDivider, (int)(tx-screenPos.x)/zoomDivider, (int)(ty-screenPos.y)/zoomDivider);
		//g.fillRect((x-screenPos.x-size/2)/zoomDivider, (y-screenPos.y-size/2)/zoomDivider, size/zoomDivider, size/zoomDivider);
	}
	
	
	public void nextFrame(){
		lifetime--;
		generateParticles();
		dir=dir+sweepFrame;
		if(lifetime==0)
			main.attacks.remove(this);
		
		hasHit=false;
		dx=(Math.cos(dir*3.1415*2)*speed);
		dy=(Math.sin(dir*3.1415*2)*speed);
		tx=x;
		ty=y;

		while(!hasHit){	
			tx=tx+dx;
			ty=ty+dy;
			
			//main.particles.add(new GlowParticle((int)tx, (int)ty));
			
			if(CollisionHandler.intersects(new Point((int)tx, (int)ty), new Point((int)(x), (int)(y))))
				hasHit=true;
			
			if(tx<0 || tx > 1000000 || ty<0 || ty > 1000000)
				hasHit=true;
		}
		//main.particles.add(new SparkParticle((int)tx, (int)ty, -(int)(Math.cos(dir*3.1415*2)*speed*100), -(int)(Math.sin(dir*3.1415*2)*speed*100)));
		
	}
	private void generateParticles(){
		if(dx != 0 || dy != 0){
			double tempx=x;
			double tempy=y;
			hasHit=false;
			int maxParticleCount=500;
	
			while(!hasHit && maxParticleCount != 0){
				tempx=tempx+dx;
				tempy=tempy+dy;
			
				main.particles.add(new GlowParticle((int)tempx, (int)tempy));
				maxParticleCount--;
				
				if(tempx == tx || tempy == ty)
					hasHit=true;
			}
		}
	}

	
	
}
